<?php
namespace app\api\service;
use think\Controller;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/5/26 0026
 * Time: 15:40
 */

class Cart extends Controller {

    /**
     * 添加商品到购物车
     * @param $param
     * @return array
     */
    public static function add_cart($param) {
        try {
            $shop_product = model('shop_product')->get(['product_id' => $param['id'], 'shop_id' => $param['shop_id']]);
            if (!$shop_product) {
                throw new \Exception('产品不存在');
            }

            if (!$shop_product->inventory) {
                throw new \Exception('商家产品库存不足');
            }

            $cart_product = model('cart')->get(['product_id' => $param['id'], 'member_id' => $param['member_id'], 'shop_id' => $param['shop_id']]);
            $model = model('cart');
            if (!$cart_product) {
                $data = [
                    'product_id' => $param['id'],
                    'shop_id' => $param['shop_id'],
                    'member_id' => $param['member_id']
                ];
                $model->save($data);
                $id = $model->id;
            } else {
                model('cart')->where(['product_id' => $param['id'], 'shop_id' => $param['shop_id'], 'member_id' => $param['member_id']])->setInc('num');
                $id = $cart_product->id;
            }

            return api_return(200, ['cart_id' => $id]);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 获取购物车列表
     * @param $param
     * @return array
     */
    public static function get_cart_list($param) {
        try {
            $shop_product = model('cart')->all(['member_id' => $param['member_id']]);

            $data = [];
            if ($shop_product) {
                foreach ($shop_product as $v) {
                    $cart_data['cart_' . $v->shop_id]['id'] = $v->shop_id;
                    $cart_data['cart_' . $v->shop_id]['name'] = $v->shop->name;
                    $cart_data['cart_' . $v->shop_id]['logo'] = get_img_url($v->shop->logo);
                    $cart_data['cart_' . $v->shop_id]['level'] = $v->shop->commentLevel() > 0 ? $v->shop->commentLevel() : 5;
                    $cart_data['cart_' . $v->shop_id]['list'][] = [
                        'id' => $v->id,
                        'product_name' => $v->product->name,
                        'thumb' => get_img_url($v->product->thumb),
                        'price' => $v->product->price,
                        'num' => $v->num,
                    ];
                }

                foreach ($cart_data as $v) {
                    $data[] = $v;
                }
            }


            return api_return(200, $data);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 修改购物车商品数量
     * @param $param
     * @return array
     */
    public static function edit_cart_product_num($param) {
        try {
            model('cart')->save(['num' => $param['num']], ['member_id' => $param['member_id'], 'id' => $param['id']]);
            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }

    /**
     * 删除购物车
     * @param $param
     * @return array
     */
    public static function del_cart($param) {
        try {
            $where = [
                'id' => ['in', $param['ids']],
                'member_id' => $param['member_id']
            ];
            model('cart')->destroy($where);
            return api_return(200, []);
        } catch (\Exception $e) {
            return api_return(0, [], $e->getMessage());
        }
    }
}